package com.max.cms0.web.contorller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.max.cms0.entity.User;
import com.max.cms0.entity.ext.userExtend;
import com.max.cms0.result.Result;
import com.max.cms0.service.UserService;
import com.max.cms0.service.impl.UserServiceImpl;
import com.max.cms0.utils.MD5Utils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @BelongsProject: cms0
 * @BelongsPackage: com.max.cms0.web.contorller
 * @Author: MHN
 * @CreateTime: 2024-11-21  10:05
 * @Description: TODO
 * @Version: 1.0
 */
@Api(tags = "用户模块")
@RestController
@Slf4j
@RequestMapping("/auth/user")
public class UserController {
    @Autowired
    private UserService userService;
    //admin使用：用户登录后首页展示个人信息需要使用
    @ApiOperation("获取用户个人信息")
    @GetMapping("/info")
    public Result getInfo(@RequestAttribute("userId") Long id)
    {
        log.info("id:{}",id);
        return Result.success(userService.queryById(id));
    }

    @ApiOperation(value = "新增用户")
    @PostMapping("/save")
    public Result save(@RequestBody User user) {
        //MD5加密
        user.setPassword(MD5Utils.MD5(user.getPassword()));
        userService.save(user);
        return Result.success("新增成功");
    }

    @ApiOperation(value = "根据id查找用户", notes = "id必须存在且有效")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id",
                    required = true, paramType = "path", dataType = "String")
    })
    @GetMapping("/queryById/{id}")
    public Result queryById(@PathVariable Long id) {
        User user = userService.queryById(id);
        return Result.success(user);
    }


    @ApiOperation(value = "分页+条件查询用户", notes = "用户中要含角色信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码",
                    required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "username", value = "用户名",
                    dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态：启用|禁用", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "roleId", value = "角色id",
                    dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "isVip", value = "是否为会员:0|1", dataType = "int", paramType = "query")
    })
    @GetMapping("/query")
    public Result query(Integer pageNum, Integer pageSize, String
            username, String status, Integer roleId, Integer isVip) {
        IPage<userExtend> p = userService.query(pageNum, pageSize, username, status, roleId, isVip);
        return Result.success(p);
    }
}
